<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
<div class="fromPage">
    <form class="layui-form layui-form-pane" lay-filter="LAY-content-articles-form">
        <div class="layui-form-item">
            <div class="layui-inline"> <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input class="layui-input" placeholder="请输入排序" type="number" name="sort" value="0">
                </div>
            </div>
            <div class="layui-inline"> <label class="layui-form-label">分类</label>
                <div class="layui-input-block">
                    <select name="category_id">
                        <option value="0">默认</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline"> <label class="layui-form-label must">标题</label>
                <div class="layui-input-inline">
                    <input class="layui-input" type="text" placeholder="请输入标题" name="title" lay-verify="required" lay-vertype="tips" >
                </div>
            </div>
            <div class="layui-inline"> <label class="layui-form-label">链接</label>
                <div class="layui-input-inline">
                    <input class="layui-input" type="text" placeholder="请输入链接" name="url">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">缩略图</label>
            <div class="layui-input-block">
                <div class="layui-upload" id="thumb"></div>
            </div>
        </div>

        <div class="layui-form-item  layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea  class="layui-textarea" id="content" name="content"></textarea>
            </div>
        </div>

        <div class="layui-form-item" pane="">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="显示">
                <input type="radio" name="status" value="0" title="隐藏" checked>
            </div>
        </div>
    </form>
</div>
</script>
<script>
    layui.data.sendParams = function(params) {
        const mode = params.mode, fromData = params.data;
        layui.use(['admin', 'baseMethods', 'fileUpload', 'tinymce'], function () {
            const {$,form,baseMethods,fileUpload,tinymce} = layui;
            fileUpload.init({
                elem: '#thumb',
                name: 'thumb',
                value: fromData.thumb||'',
            });
            fromData && form.val('LAY-content-articles-form', fromData);
            let edit = tinymce.render({
                elem: "#content",
                height: 500,
                relative_urls: false,
                convert_urls: false,
                setup: function(editor) {
                    editor.on('change', function() {
                        editor.save();
                    });
                },
                urlconverter_callback: function (url, node, onSave, name) {
                    if (node === 'img' && url.startsWith('blob:')) {
                        tinymce.activeEditor && tinymce.activeEditor.uploadImages()
                    }
                    return url;
                },
                images_upload_handler : function (blobInfo, succFun, failFun) {
                    let formData = new FormData();
                    formData.append('file', blobInfo.blob());
                    baseMethods.request({
                        url: '/upload',
                        type: 'post',
                        data: formData,
                        processData: false,
                        contentType: false,
                        done: function (result) {
                            return result.code === 200 ? succFun(AppGeneral.storage(result.data.file_path)) : failFun(result.message);
                        },
                    })
                }
            });
            function getCate() {
                return new Promise(function(resolve, reject) {
                    baseMethods.request({
                        url: '/content/column/list',
                        done: function (result) {
                            if (result.code === 200) {
                                let data = result.data.rows;
                                if (data.length > 0) {
                                    data.map(item => $('select[name=category_id]').append(new Option(item.name, item.id)));
                                    form.render("select", 'LAY-content-articles-form');
                                }
                            }
                            resolve();
                        },
                    })
                })
            }
            if (mode === 'add') {
                getCate().then(() => {
                    let {categoryId} = JSON.parse(sessionStorage.getItem('ArticleCache'));
                    categoryId && form.val('LAY-content-articles-form', {categoryId});
                });
            }else if (mode === 'edit') {
                getCate().then(() => {
                    baseMethods.request({
                        url: '/content/articles/'+fromData.id+'/edit',
                        done: function (result) {
                            if (result.code === 200) {
                                let data = result.data;
                                edit.setContent(data.content)
                                form.val('LAY-content-articles-form', data);
                            }
                        }
                    })
                });
            }
        })
    }
</script>
